﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ChooseQuestionType : MonoBehaviour {
    public Transform m_ToggleGroup, m_ScrollPart,m_OptionContent;
    public Option m_OptionPrefab;
    public GameObject m_ChoosePart;

    private int m_type;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    public void Reset() {
        GetComponent<Dropdown>().value = 0;
        for (int i = 0; i < m_OptionContent.childCount; i++) {
            QuestionMgr.s_Instance.m_OptionList.Clear();
            Destroy(m_OptionContent.GetChild(i).gameObject);
        }
        m_ChoosePart.SetActive(false);
    }
    public void OnTypeDropDownValueChange(int value) {
        m_type = value;
        switch (value) {
            case 0:
                m_ChoosePart.SetActive(false);
                break;
            case 1:
                m_ChoosePart.SetActive(true);
                m_ScrollPart.SetParent(m_ToggleGroup);
                foreach (var item in QuestionMgr.s_Instance.m_OptionList) {
                    item.m_Toggle.isOn = false;
                    item.m_Toggle.group = m_ToggleGroup.GetComponent<ToggleGroup>();
                    item.m_JumpIDInput.gameObject.SetActive(true);
                }
                break;
            case 2:
                m_ChoosePart.SetActive(true);
                m_ScrollPart.SetParent(m_ChoosePart.transform);
                foreach(var item in QuestionMgr.s_Instance.m_OptionList) {
                    item.m_Toggle.group = null;
                    item.m_JumpIDInput.gameObject.SetActive(false);
                   // item.transform.SetParent(m_ScrollPart);
                }
                break;
            case 3:
                m_ChoosePart.SetActive(false);
                break;
        }
    }
    public void OnCountValueChange(int value) {
        
        for (int i = 0; i < m_OptionContent.childCount; i++) {
            QuestionMgr.s_Instance.m_OptionList.Clear();
            Destroy(m_OptionContent.GetChild(i).gameObject);
        }
        if(m_type == 1) {
            m_ScrollPart.SetParent(m_ToggleGroup);
        } else {
            m_ScrollPart.SetParent(m_ScrollPart);
        }
        if (value == 0)
            return;
        for (int i = 0; i < value+1; i++) {
            Option option = Instantiate(m_OptionPrefab) as Option;
            option.transform.SetParent(m_OptionContent);
            option.transform.localScale = new Vector3(1f, 1f, 1f);
            if(m_type == 1) {
                option.m_Toggle.group = m_ToggleGroup.GetComponent<ToggleGroup>();
            } else {
                option.m_Toggle.group = null;
            }
            
            QuestionMgr.s_Instance.m_OptionList.Add(option);
        }
    }
    public void ChangeContentByIndex(int index, int isRight,int score,string stem,string jumpid) {
        Option option = QuestionMgr.s_Instance.m_OptionList[index];
        option.m_JumpIDInput.text = jumpid;
        option.SetToggleState(isRight);
        option.m_OptionContentText.GetComponentInParent<InputField>().text = stem;
        option.SetScore(score);
    }
}
